﻿Ext.define('HIT.controller.TableManagementController', {
	extend : 'Ext.app.Controller',
	models : [ 'TableManager' ],
	stores : [ 'TableManager' ],
	views : [ 'SetTableMgtGrid', 'SetTableMgtAddWin',
			'SetTableMgtEditWin' ],

	onButtonClick : function(button, e, options) {

		// 添加按钮
		if (button.id == 'set-table-add') {
			p = Share.createPanelByAlias('widget.tablemanagementaddpanel');
			Share.showPanelAsWindow(p);
		} 
		// 编辑按钮
		else if (button.id == 'set-table-edit') {
			var grid = Ext.getCmp('tab-set-tablemanagement');
			if (grid == undefined) {
				return;
			}
			// 确认一览记录选中情况
			var selModel = grid.getSelectionModel();
			// 有记录被选中
			if (selModel.hasSelection()) {
				// 选中了多条记录
				if (selModel.getCount() != 1) {
					Share.ShowMessage('I0009','修改');
					return;
				}
				// 进入选中记录的编辑页面
				else{
					var model = selModel.getSelection()[0];
					p = Share.createPanelByAlias('widget.tablemanagementeditpanel');
					Ext.getCmp('t_name_edit').setValue(model.get('t_name'));
					Ext.getCmp('t_name_cn_edit').setValue(model.get('t_name_cn'));
					Ext.getCmp('t_name_en_edit').setValue(model.get('t_name_en'));
					Share.showPanelAsWindow(p);
				}
			}
			// 没有选中任何记录
			else{
				Share.ShowMessage('I0008');
			}
		}
		// 删除按钮
		else if(button.id == 'set-table-delete'){
			var grid = Ext.getCmp('tab-set-tablemanagement');
			if (grid == undefined) {
				return;
			}
			var selModel = grid.getSelectionModel();
			var row = selModel.getSelection();
			if (selModel.hasSelection()) {
				Ext.MessageBox.confirm(Share.T('提示'),Share.T('是否确认删除？'),function(btn){
					if(btn == 'yes'){
						var model = selModel.getSelection();
						var t_name = [];
	                	for(var i=0;i<model.length;i++){
	                		t_name.push(model[i].get('t_name'));
	                    }
						Share.AjaxRequest({
							url : ctx + '/tableManagerDelete',  // 表名数据删除
							params : {
								t_name : t_name.join(",")
							},
							callback : function(json) {
								if (json.resultcode == '0') {
									Share.ShowMessage('I0006','删除');
									var toolbar=Ext.getCmp('tablemanagementtoolbar');
									var fromRecord=toolbar.getPageData().fromRecord;
									var toRecord=toolbar.getPageData().toRecord;
									if(toRecord-fromRecord+1==row.length){
										toolbar.movePrevious();
									}
									Ext.getCmp('tab-set-tablemanagement').getStore().reload();
								} else {
									Share.ShowMessage('I0007','删除');
								}
							},
							falseFun : function(json) {
								Share.ShowMessage('I0007','表名');
							}
						});
					}else{
						return;
					}
				});
			}else{
				Share.ShowMessage('I0008');
				return;
			}
		}
	},
	
	// 表名新增/修改页面 保存按钮
	save : function(win) {
		var functionId = Ext.getCmp('functionId').value;
		// 添加表名
		if(functionId == 1){
			if (!Ext.getCmp('table_add').getForm().isValid()) {
				return;
			}
			var t_name = Ext.getCmp('t_name_add').getValue();
			var t_name_cn = Ext.getCmp('t_name_cn_add').getValue();
			var t_name_en = Ext.getCmp('t_name_en_add').getValue();
			if(t_name == '' || t_name == null){
				Share.ShowMessage('I0010', '表名称');
				return;
			}
			if(t_name_cn == '' || t_name_cn == null){
				Share.ShowMessage('I0010', '表中文名称');
				return;
			}
			if(t_name_en == '' || t_name_en == null){
				Share.ShowMessage('I0010', '表英文名称');
				return;
			}
			if(t_name_cn.length > 50){
				Share.ShowMessage('E0003', '表中文名称', '50');
				return;
			}
			if(t_name_en.length > 50){
				Share.ShowMessage('E0003', '表英文名称', '50');
				return;
			}
			Share.AjaxRequest({
				url : ctx + '/tableManagerAdd',  // 保存表名信息
				params : {
					t_name : t_name,
					t_name_cn : t_name_cn,
					t_name_en : t_name_en
				},
				callback : function(json) {
					if (json.resultcode == '0') {
						win.close();
						Share.ShowMessage('I0006', '添加');
						Ext.getCmp('tab-set-tablemanagement').getStore().reload();
					} else {
						Share.ShowMessage('I0007', '添加');
					}
				},
				falseFun : function(json) {
					Share.ShowMessage('I0007', '表名');
				}
			});
		}
		// 编辑表名
		else if(functionId == 0){
			if (!Ext.getCmp('table_edit').getForm().isValid()) {
				return;
			}
			var t_name = Ext.getCmp('t_name_edit').getValue();
			var t_name_cn = Ext.getCmp('t_name_cn_edit').getValue();
			var t_name_en = Ext.getCmp('t_name_en_edit').getValue();
			if(t_name_cn == '' || t_name_cn == null){
				Share.ShowMessage('I0010', '表中文名称');
				return;
			}
			if(t_name_en == '' || t_name_en == null){
				Share.ShowMessage('I0010', '表英文名称');
				return;
			}
			if(t_name_cn.length > 50){
				Share.ShowMessage('E0003', '表中文名称', '50');
				return;
			}
			if(t_name_en.length > 50){
				Share.ShowMessage('E0003', '表英文名称', '50');
				return;
			}
			Share.AjaxRequest({
				url : ctx + '/tableManagerEdit',  // 表名编辑
				params : {
					t_name : t_name,
					t_name_cn : t_name_cn,
					t_name_en : t_name_en
				},
				callback : function(json) {
					if (json.resultcode == '0') {
						win.close();
						Share.ShowMessage('I0006', '编辑');
						Ext.getCmp('tab-set-tablemanagement').getStore().reload();
					} else {
						Share.ShowMessage('I0007', '编辑');
					}
				},
				falseFun : function(json) {
					Share.ShowMessage('I0007','表名');
				}
			});
		}
	},
	init : function(application) {
		this.control({
			"button" : {
				click : this.onButtonClick
			}
		});
	}
});
